package cn.swenty.backstage.framework.common.util;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

/**
 * 日期操作工具类
 * 
 * @since Apr 16, 2009 5:49:50 PM
 */
public class DateUtil {
	private static Logger log=Logger.getLogger(DateUtil.class);
	

	public final static long MINUTE_MILLIS = 60000L;
	
	public final static long HOUR_MILLIS = 3600000L;
	
	public final static long DAY_MILLIS = 24*3600000L;
	/**判断一个时间是只有年月日*/
	public static boolean isDate(Date date) {
		DateTime d=new DateTime(date,DateTime.YEAR_TO_DAY);
		return d.getTime()==date.getTime();
	}
	
	/**将一个date的时间部分去掉*/
	public static Date toDate(Date date) {
		return new DateTime(date,DateTime.YEAR_TO_DAY);
	}
	

	public static Timestamp getTimestamp(){
		return new Timestamp(System.currentTimeMillis());
	}
	
	public static Timestamp getTimestamp(Date date){
		return new Timestamp(date.getTime());
	}
	
	public static Date getDate(String dateStr,String format){
		DateFormat dateFormat = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = dateFormat.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		return date;
	} 
	
	
	public static String getDateDelay(Date startDate,long delay){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		long myTime = startDate.getTime() + delay * 24 * 60 * 60 * 1000; ;
		return format.format(new Date(myTime)); 
	}
	
	public static int getDayByTwoDate(Date startDate, Date endDate){
		int day = 0;
		long millTime = endDate.getTime() - startDate.getTime();
		day = (int)(millTime / (24 * 60 * 60 * 1000)); 
		return day;
	}
	
	public static Date getDateFromString(String dateStr){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos = new ParsePosition(0); 
		Date strtodate = formatter.parse(dateStr, pos);
		return strtodate;
	}
	
	public static Date getDateFromMMDD(String MMDD){
		Date date = new Date(System.currentTimeMillis());
		int year = date.getYear() + 1900;
		String dateStr = year + "-" + MMDD;
		return getDateFromString(dateStr);
	}
	
	public static String getHHMMString(int hour,int minute){
		String h = "";
		if(hour < 10){
			h = "0"+hour;
		}else{
			h = ""+hour;
		}
		String m = "";
		if(minute < 10){
			m = "0"+minute;
		}else{
			m = "" + minute;
		}
		return h + m;
	}
	
	/**
	 * 传入一个客户的出生年月日的时间对象，和系统时间比较后返回其年龄的方法
	 * @author 贾小元
	 * @param Date birthday
	 * @return int age
	 * */
	public static int getAge(Date birthday){
		int returnInt = 0;
		DateTime birthdatetime = new DateTime(birthday,DateTime.YEAR_TO_SECOND);
		// 今天
		DateTime dateTime = new DateTime(new Date(),DateTime.YEAR_TO_SECOND);
		// 开始计算年龄
		int birthYear = birthdatetime.getYear();		// 出生年份
		int birthMonth = birthdatetime.getMonth(); 	// 出生月份
		int birthDay = birthdatetime.getDate();		// 出生日干
		int todayYear = dateTime.getYear(); 	// 今天的年份
		int todayMonth = dateTime.getMonth();	// 今天的月份
		int todayDay = dateTime.getDate();		// 今天的日干
		returnInt = todayYear - birthYear;
		if(returnInt <= 0 ){  // 如果生日就在今年
			returnInt = 0;
			return returnInt;
		}
		if(todayYear > birthYear){
			// 出生月份大于本月时，要减1年
			if(birthMonth > todayMonth){
				returnInt --;
			}else if(birthMonth == todayMonth){ // 出生月份等于本月的，要看日干
				if(birthDay > todayDay){
					returnInt --;
				}
			}
		}
		return returnInt;
	}
	
	public static Date toDate(String time,String dateFormat){
		if(StringUtil.isNull(time)){
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		try {
			return sdf.parse(time);
		} catch (ParseException e) {
			log.warn("无法将" + time + "转换为日期");
			return null;
		}
	}
	
	public static Date toDate(String time){
		String dateFormat = "yyyy-MM-dd";
		String datetimeFormat ="yyyy-MM-dd HH:mm:ss";
		if (time == null || time.equals("")) {
			return null;
		}
		SimpleDateFormat sdf = null;
		if (time.replaceAll("\\d", "").equals(dateFormat.replaceAll("\\w", ""))) {
			sdf = new SimpleDateFormat(dateFormat);
		} else {
			sdf = new SimpleDateFormat(datetimeFormat);
		}
		try {
			return sdf.parse(time);
		} catch (ParseException e) {
			log.warn("无法将" + time + "转换为日期");
			return null;
		}
	}
	
	public static String second2Time(BigDecimal second){
		int h = (int) Math.floor(second.intValue()/3600);
		int m = (int) Math.floor((second.intValue() - h*3600)/60);
		int s = (int) Math.floor(second.intValue() - h*3600 - m*60);
		return h+":"+(m>9?m:("0"+m))+":"+(s>9?s:("0"+s));
	}
	
	public static String second2Time(int second){
		int h = (int) Math.floor(second/3600);
		int m = (int) Math.floor((second - h*3600)/60);
		int s = (int) Math.floor(second - h*3600 - m*60);
		return h+":"+(m>9?m:("0"+m))+":"+(s>9?s:("0"+s));
	}
	
	public static String dateToString(Date date,String format){
		if(date == null){
			return null;
		}
		SimpleDateFormat df=new SimpleDateFormat(format);
		String strDate=df.format(date);
		return strDate;
	}
	
	public static Date getNextDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		date = calendar.getTime();
		return date;
	}
	public static Date dateAdd(Date date,  int type,int monut){
		Calendar calendar = new SimpleDateFormat().getCalendar();
		calendar.setTime(date);
		calendar.add(type, monut);
		return calendar.getTime();
	}
	public static Date getMonthFirstDay(Date date){
		  Calendar calendar = Calendar.getInstance();
		    calendar.setTime(date);
		    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		    return calendar.getTime();
	}
	
	public static Date getMonthLastDay(Date date){
	    Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
	    return calendar.getTime();
	}


}
